home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 376_04 / os2tool.003 / TOUCH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-05  |  3.2 KB  |  130 lines

  1. /*
  2. * TOUCH.C - Change the last modification date and time of a file.
  3. *
  4. * PROGRAMMER:        Martti Ylikoski
  5. * CREATED:        8.12.1990
  6. */
  7. static char* VERSION="Version 1.1. Copyright (c) Martti Ylikoski. 1990, 1991." ;
  8. /*
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <param.h>
  16. #include <paramstd.h>
  17. #define INCL_DOS
  18. #include <os2.h>
  19.  
  20. static char *progname, *fname    ;
  21.  
  22. extern unsigned long pflags ;
  23.  
  24. ParamEntry pentry[12] = {
  25.      "P", &ParamSetPause, 0,
  26.      "F", &ParamSetFold, 0,
  27.      "V", &ParamSetVerbose, 0,
  28.      "R", &ParamSetReport, 0,
  29.      "S", &ParamSetSubDirs, 0,
  30.      "?", &ParamSetHelp, 1,
  31.      "H", &ParamSetHelp, 1,
  32.      "NOD", &ParamSetNoDefault, 0,
  33.      "TEST", &ParamSetTest, 0,
  34.      "Y", &ParamSetYes, 0,
  35.      "N", &ParamSetTest, 0,
  36.      "\0", NULL, 0
  37. } ;
  38.  
  39. ParamBlock params = {
  40.     "/-",   IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
  41.     pentry
  42. } ;
  43.  
  44. /* local prototypes */
  45. static int touch(char *fname) ;
  46.  
  47. int main(int argc, char *argv[])
  48. {
  49. int i ;
  50.  
  51.    progname = argv[0] ;
  52.  
  53.    ParamHandle(¶ms, &argc, argv) ;
  54.  
  55.    if (pflags & PA_HELP || argc == 1)
  56.    {
  57.       printf("%s - change the last modification time and date of a file.\n", progname) ;
  58.       puts(VERSION) ;
  59.       puts("Usage: touch file(s) [/H | /?]") ;
  60.       puts("Where,") ;
  61.       puts("     /H,/? = Help") ;
  62.       return( 0 ) ;
  63.    }
  64.  
  65.    fname = argv[1] ;
  66.  
  67.    for (i = 1; i <argc; i++)
  68.       touch(argv[i]) ;
  69.    return( 0 ) ;
  70. }
  71.  
  72. static int touch(char *fname)
  73. {
  74. HFILE hf ;
  75. int c ;
  76. USHORT action, ret ;
  77. FILESTATUS fstat ;
  78. DATETIME dtim ;
  79.       
  80.    /* open file */
  81.  
  82.    if ((ret = DosOpen(fname, &hf, &action, 0L, FILE_NORMAL,
  83.        FILE_OPEN|FILE_CREATE,OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYREADWRITE, 0L)) != 0)
  84.    {
  85.       printf("%s error %d opening file %s\n", progname, ret, fname) ;
  86.       DosClose(hf) ;
  87.       return( (int) ret ) ;
  88.     }
  89.  
  90.    if (( ret = DosQFileInfo(hf, 1, (PVOID) &fstat, sizeof(fstat)))    != 0)
  91.    {
  92.       printf("%s error in DosSetFileInfo\n", progname) ;
  93.       DosClose(hf) ;
  94.       return( (int) ret ) ;
  95.     }
  96.  
  97.    if (( ret = DosGetDateTime(&dtim)) != 0)
  98.    {
  99.       printf("%s error %d in DosGetDateTime\n", progname, ret) ;
  100.       DosClose(hf) ;
  101.       return( (int) ret ) ;
  102.     }
  103.  
  104.    fstat.fdateLastAccess.day  = dtim.day  ;
  105.    fstat.fdateLastAccess.month = dtim.month ;
  106.    fstat.fdateLastAccess.year =  (unsigned) dtim.year - 1980 ; /* years represented relative to 1980 */
  107.    fstat.ftimeLastAccess.twosecs = dtim.seconds/2 ;
  108.    fstat.ftimeLastAccess.minutes = dtim.minutes ;
  109.    fstat.ftimeLastAccess.hours = dtim.hours ;
  110.    fstat.fdateLastWrite = fstat.fdateLastAccess ;
  111.    fstat.ftimeLastWrite = fstat.ftimeLastAccess ;
  112.    if ((ret = DosSetFileInfo(hf, 1, (PBYTE) &fstat, (USHORT) sizeof(fstat))) != 0)
  113.    {
  114.       if (( ret = DosNewSize(hf, fstat.cbFile)) != 0) /* clever no-op = change files size
  115.                              to what it already was. This is needed because in DOS-
  116.                             compatility mode DosSetFileInfo does not work in 1.21 */
  117.       {
  118.  
  119.      printf("%s unable to set time\n", progname) ;
  120.      DosClose(hf) ;
  121.      return( (int) ret ) ;
  122.        }
  123.     }
  124.  
  125.    /* close file */
  126.    DosClose(hf) ;
  127.    return( 0 ) ;
  128.  
  129. }
  130.